// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/stmts/update_stmt.h>
#include <cantor/transform/dqls/table_ref.h>
#include <cantor/transform/dmls/assignment.h>
#include <cantor/transform/expr.h>
#include <cantor/transform/dqls/order_by_clause.h>
#include <cantor/transform/dqls/limit_clause.h>
#include <cantor/transform/dqls/having_clause.h>

#include "cantor/transform/dqls/where_clause.h"

namespace cantor::transform {
    void TransUpdateStmt::resolve_ast(const cantor::parser::UpdateStmt *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }

        /// priority
        update_stmt.set_priority(node->priority);
        update_stmt.set_is_ignore(node->is_ignore);
        // table_refs
        if (node->table_refs) {
            status = TransTableRef::resolve_to(node->table_refs, update_stmt.mutable_table_refs());
        }
        if (!status.ok()) {
            return;
        }

        // set_list
        auto list = update_stmt.mutable_set_list();
        for (auto i = 0; i < node->set_list.size(); ++i) {
            status = TransAssignment::resolve_to(node->set_list[i], list->Add());
            if (!status.ok()) {
                return;
            }
        }
        /// where
        if (node->where) {
            status = TransWhereClause::resolve_to(node->where, update_stmt.mutable_where());
            if (!status.ok()) {
                return;
            }
        }

        /// order
        if (node->order) {
            status = TransOrderByClause::resolve_to(node->order, update_stmt.mutable_order());
            if (!status.ok()) {
                return;
            }
        }

        /// limit
        if (node->limit) {
            status = TransLimitClause::resolve_to(node->limit, update_stmt.mutable_limit());
            if (!status.ok()) {
                return;
            }
        }
        set_proto_location(node, update_stmt.mutable_location());
        set_proto_status(update_stmt.mutable_status());
    }

    const turbo::Status &TransUpdateStmt::check() {
        if (!status.ok()) {
            return status;
        }
        if (!update_stmt.has_location()) {
            make_missing_required_field("location");
        }
        return status;
    }

    turbo::Status TransUpdateStmt::resolve_to(turbo::Nonnull<const cantor::parser::UpdateStmt *> arg,
                                              turbo::Nonnull<pb::ast::UpdateStmt *> out) {
        TransUpdateStmt trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.update_stmt);
        return status;
    }
} // namespace cantor::transform
